package com.example.adapter.tuijianadapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.constant.tuijian.ResultBean;
import com.example.ui.R;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 * 创建时间 2017/9/28
 * 作者 fengxn
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 */

public class TuijianTitleApdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{

    private List<ResultBean> data;
    private Context context;

    public static final int ITEM_TYPE01 = 0;//recommend
    public static final int ITEM_TYPE02 = 1;//live
    public static final int ITEM_TYPE03 = 2;//bangumi_2
    public static final int ITEM_TYPE04 = 3;//activity
    public static final int ITEM_TYPE05 = 4;//weblink
    public static final int ITEM_TYPE06 = 5;//region

    public TuijianTitleApdapter(Context context, List<ResultBean> data)
    {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType)
        {
            case 3:
                viewHolder = new ActivityViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tuijian_activity, parent, false));
                break;
            case 2:
                viewHolder = new Bangumi2ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tuijian_bangumi_2, parent, false));
                break;
            case 1:
                viewHolder = new LiveViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tuijian_live, parent, false));
                break;
            case 0:
                viewHolder = new RecommendViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tuijian_recommend, parent, false));
                break;
            case 5:
                viewHolder = new RegionViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tuijian_region, parent, false));
                break;
            case 4:
                viewHolder = new WeblinkViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tuijian_weblink, parent, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        if (holder instanceof RecommendViewHolder)
        {
            ((RecommendViewHolder) holder).tv1.setText(data.get(position).getHead().getTitle());
            ((RecommendViewHolder) holder).rlv_recommend.setLayoutManager(new GridLayoutManager(context, 2, LinearLayout.VERTICAL, false));
            TuijianApdapter apdapter = new TuijianApdapter(context, data.get(position).getBody());
            ((RecommendViewHolder) holder).rlv_recommend.setAdapter(apdapter);
        }
        if (holder instanceof LiveViewHolder)
        {
            ((LiveViewHolder) holder).tv1.setText(data.get(position).getHead().getTitle());
            ((LiveViewHolder) holder).tv2.setText(data.get(position).getHead().getCount());
            ((LiveViewHolder) holder).rlv_live.setLayoutManager(new GridLayoutManager(context, 2, LinearLayout.VERTICAL, false));
            TuijianApdapter apdapter = new TuijianApdapter(context, data.get(position).getBody());
            ((LiveViewHolder) holder).rlv_live.setAdapter(apdapter);
        }
        if (holder instanceof Bangumi2ViewHolder)
        {
            ((Bangumi2ViewHolder) holder).tv1.setText(data.get(position).getHead().getTitle());
            ((Bangumi2ViewHolder) holder).rlv_bangumi_2.setLayoutManager(new GridLayoutManager(context, 2, LinearLayout.VERTICAL, false));
            TuijianApdapter apdapter = new TuijianApdapter(context, data.get(position).getBody());
            ((Bangumi2ViewHolder) holder).rlv_bangumi_2.setAdapter(apdapter);
        }
        if (holder instanceof RegionViewHolder)
        {
            ((RegionViewHolder) holder).tv1.setText(data.get(position).getHead().getTitle());
            ((RegionViewHolder) holder).rlv_region.setLayoutManager(new GridLayoutManager(context, 2, LinearLayout.VERTICAL, false));
            TuijianApdapter apdapter = new TuijianApdapter(context, data.get(position).getBody());
            ((RegionViewHolder) holder).rlv_region.setAdapter(apdapter);
        }
        if (holder instanceof ActivityViewHolder)
        {
            ((ActivityViewHolder) holder).tv1.setText(data.get(position).getHead().getTitle());
            ((ActivityViewHolder) holder).rlv_activity.setLayoutManager(new GridLayoutManager(context, 2, LinearLayout.VERTICAL, false));
            TuijianApdapter apdapter = new TuijianApdapter(context, data.get(position).getBody());
            ((ActivityViewHolder) holder).rlv_activity.setAdapter(apdapter);
        }
        if (holder instanceof WeblinkViewHolder)
        {
            Picasso.with(context)
                    .load(data.get(position).getBody().get(0).getCover())
                    .placeholder(R.mipmap.bili_drawerbg_logined)
                    .into(((WeblinkViewHolder) holder).iv_weblink);
        }
    }

    @Override
    public int getItemViewType(int position)
    {
        int type = 0;
        if (data.get(position).getType().equals("recommend"))
        {
            type = ITEM_TYPE01;
        }
        if (data.get(position).getType().equals("live"))
        {
            type = ITEM_TYPE02;
        }
        if (data.get(position).getType().equals("bangumi_2"))
        {
            type = ITEM_TYPE03;
        }
        if (data.get(position).getType().equals("activity"))
        {
            type = ITEM_TYPE04;
        }
        if (data.get(position).getType().equals("weblink"))
        {
            type = ITEM_TYPE05;
        }
        if (data.get(position).getType().equals("region"))
        {
            type = ITEM_TYPE06;
        }
        return type;

    }

    @Override
    public int getItemCount()
    {
        return data.size();
    }

    public class ActivityViewHolder extends RecyclerView.ViewHolder
    {
        public TextView tv1;
        public RecyclerView rlv_activity;

        public ActivityViewHolder(View itemView)
        {
            super(itemView);
            this.tv1 = (TextView) itemView.findViewById(R.id.item_tuijian_activity_title);
            this.rlv_activity = (RecyclerView) itemView.findViewById(R.id.rlv_tuijian_activity);
        }
    }

    public class Bangumi2ViewHolder extends RecyclerView.ViewHolder
    {
        public TextView tv1;
        public RecyclerView rlv_bangumi_2;

        public Bangumi2ViewHolder(View itemView)
        {
            super(itemView);
            this.tv1 = (TextView) itemView.findViewById(R.id.item_tuijian_bangumi2_title);
            this.rlv_bangumi_2 = (RecyclerView) itemView.findViewById(R.id.rlv_tuijian_bangumi_2);
        }
    }

    public class LiveViewHolder extends RecyclerView.ViewHolder
    {
        public TextView tv1;
        public TextView tv2;
        public RecyclerView rlv_live;

        public LiveViewHolder(View itemView)
        {
            super(itemView);
            this.tv1 = (TextView) itemView.findViewById(R.id.item_tuijian_live_title);
            this.tv2 = (TextView) itemView.findViewById(R.id.item_tuijian_live_count);
            this.rlv_live = (RecyclerView) itemView.findViewById(R.id.rlv_tuijian_live);
        }
    }

    public class RecommendViewHolder extends RecyclerView.ViewHolder
    {
        public TextView tv1;
        public RecyclerView rlv_recommend;

        public RecommendViewHolder(View itemView)
        {
            super(itemView);
            this.tv1 = (TextView) itemView.findViewById(R.id.item_tuijian_recommend_title);
            this.rlv_recommend = (RecyclerView) itemView.findViewById(R.id.rlv_tuijian_recommend);
        }
    }

    public class RegionViewHolder extends RecyclerView.ViewHolder
    {
        public TextView tv1;
        public RecyclerView rlv_region;

        public RegionViewHolder(View itemView)
        {
            super(itemView);
            this.tv1 = (TextView) itemView.findViewById(R.id.item_tuijian_region_title);
            this.rlv_region = (RecyclerView) itemView.findViewById(R.id.rlv_tuijian_region);
        }
    }

    public class WeblinkViewHolder extends RecyclerView.ViewHolder
    {
        ImageView iv_weblink;

        public WeblinkViewHolder(View itemView)
        {
            super(itemView);
            iv_weblink = (ImageView) itemView.findViewById(R.id.item_tuijian_weblink_iv);
        }
    }

}
